package day14.homework01.test06;

/**
 * @author Xiao
 * @date 2021/8/6
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * 问题：
 *      * 	已知奖池中有以下奖励：int[] arr = {85, 100, 60, 10, 50, 20, 22,8};
 *      * 	使用两个线程实现随机抽奖。
 *      * 		要求：
 *      * 			1. 每抽一个打印一个奖金，每个奖金只能抽取一次。
 *      * 			2. 抽完奖后统计每个线程抽到的总奖金
 */
public class Bonus implements Runnable{
    private static final int[] arr = {85, 100, 60, 10, 50, 20, 22,8};
    private int sum1;
    private int sum2;
    private ArrayList<Integer> list = new ArrayList<>(List.of(0, 1, 2, 3, 4, 5, 6, 7));
    private ArrayList<Integer> list1 = new ArrayList<>();
    private Random r = new Random();

    /**这是邓超同学的方法*/
    public void method1() {
        while (true) {
            synchronized (this) {
                if ("抽奖箱1".equals(Thread.currentThread().getName())) {
                    if (list1.size() == arr.length) {
                        System.out.println("抽奖箱1总共抽中了" + sum1 + "元奖金");
                        break;
                    }
                    while (true) {
                        int index = r.nextInt(arr.length);
                        if (!list1.contains(arr[index])) {
                            System.out.println("抽奖箱1抽中了" + arr[index] + "元奖金。。。");
                            sum1 += arr[index];
                            list1.add(arr[index]);
                            break;
                        }
                    }
                }
                if ("抽奖箱2".equals(Thread.currentThread().getName())) {
                    if (list1.size() == arr.length) {
                        System.out.println("抽奖箱2总共抽中了" + sum2 + "元奖金");
                        break;
                    }
                    while (true) {
                        int index = r.nextInt(arr.length);
                        if (!list1.contains(arr[index])) {
                            System.out.println("抽奖箱2抽中了" + arr[index] + "元奖金。。。");
                            sum2 += arr[index];
                            list1.add(arr[index]);
                            break;
                        }
                    }
                }
            }
        }
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Collections.shuffle(list);
                if ("抽奖箱1".equals(Thread.currentThread().getName())) {
                    if (list.size() == 0) {
                        System.out.println("抽奖箱1总共抽中了" + sum1 + "元奖金");
                        break;
                    }
                    Integer integer = list.get(0);
                    System.out.println("抽奖箱1抽中了" + arr[integer] + "元奖金。。。");
                    list.remove(0);
                    sum1 += arr[integer];
                }
                if ("抽奖箱2".equals(Thread.currentThread().getName())) {
                    if (list.size() == 0) {
                        System.out.println("抽奖箱2总共抽中了" + sum2 + "元奖金");
                        break;
                    }
                    Integer integer = list.get(0);
                    System.out.println("抽奖箱2抽中了" + arr[integer] + "元奖金。。。");
                    list.remove(0);
                    sum2 += arr[integer];
                }
            }
        }
    }
}
